//
//  HSSystemNoticeVC.m
//  HolyShit
//
//  Created by Robin on 2017/4/3.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSSystemNoticeVC.h"

static NSString *const HSPushConfigKey = @"HSPushConfigKey";

@interface HSSystemNoticeVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<NSString *> *datas;

@end

@implementation HSSystemNoticeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"系统通知";
    [self.view addSubview:self.tableView];
    self.datas = [[NSUserDefaults standardUserDefaults] objectForKey:HSPushConfigKey];
    if (!self.datas) {
        self.datas = @[@"0",@"0",@"0",@"0",@"0"].mutableCopy;
    }
    [self.tableView reloadData];
}

- (void)viewDidDisappear:(BOOL)animated
{
    NSArray<UITableViewCell *> *cells = [self.tableView visibleCells];
    __block NSMutableArray<NSString *> *datas = @[].mutableCopy;
    [cells enumerateObjectsUsingBlock:^(UITableViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UISwitch *sw = [obj viewWithTag:2333];
        [datas addObject:sw.isOn ? @"1" : @"0"];
    }];
    [[NSUserDefaults standardUserDefaults] setObject:datas forKey:HSPushConfigKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [super viewDidDisappear:animated];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
    view.backgroundColor = HEXCOLOR(HSColorBgLight);
    UILabel *lable = [UILabel labelWithFrame:CGRectMake(15, 0, ScreenWidth - 25, 44) font:MYFont(Font26) color:HEXCOLOR(HSColorGray)];
    lable.numberOfLines = 2;
    if (section == 0) {
        lable.text = @"如果你要关闭或开启应用新消息提醒，请再iPhone的\"设置\"-\"通知\"功能中操作";
    }
    else if(section == 1){
        lable.text = @"关闭后资讯的新通知将不会通知你";
    }
    else{
        lable.text = @"当应用在运行时，你可以设置是否需要声音或震动";
    }
    [view addSubview:lable];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellid = @"cellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellid];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectMake(ScreenWidth - 80, 5, 80, 34)];
        sw.onTintColor = HEXCOLOR(appNavColor);
        sw.tag = 2333;
        [cell addSubview:sw];
    }
    UISwitch *sw = [cell viewWithTag:2333];
    if (indexPath.section == 0) {
        sw.hidden = YES;
        cell.detailTextLabel.text = [[UIApplication sharedApplication] currentUserNotificationSettings].types  == UIUserNotificationTypeNone ? @"已关闭" : @"已开启";
        cell.textLabel.text = @"新消息通知";
    }
    else{
        sw.hidden = NO;
        cell.detailTextLabel.text = @" ";
        NSUInteger idx = (indexPath.section - 1) * 2 + indexPath.row + 1;
        sw.on = self.datas[idx].integerValue == 1;
        if (indexPath.section == 1) {
            cell.textLabel.text = indexPath.row == 0 ? @"应用通知" : @"资讯新消息通知";
        }
        else{
            cell.textLabel.text = indexPath.row == 0 ? @"声音" : @"震动";
        }
    }
    if (indexPath.row > 0) {
        [cell showLineForDirection:MYDirectionTop leftPad:15];
    }
    else{
        [cell hideLineForDirection:MYDirectionTop];
    }
    [cell layoutSubviews];
    return cell;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - NavBarHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = HEXCOLOR(HSColorBgLight);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

@end
